package cn.school.controller.attendance;

import cn.school.attendance.dormitory.entity.AttendanceRules;
import cn.school.attendance.dormitory.service.AttendanceRulesService;
import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author: lx
 * @date: 2023/9/5 9:55
 * @description:
 */
@Api(tags = "attendance-rules => 考勤规则")
@RestController
@RequestMapping("/attendance/rules")
public class AttendanceRulesController {

    @Autowired
    AttendanceRulesService attendanceRulesService;

    @PostMapping("/findPageList")
    public CommonResult findRulesPageList(@RequestBody AttendanceRules query){
        IPage<AttendanceRules> page = attendanceRulesService.findRulesPageList(query);
        return CommonResult.success(CommonPage.restPage(page));
    }

    @PostMapping("/add")
    public CommonResult addRules(@RequestBody AttendanceRules rules){
        int num = attendanceRulesService.addRules(rules);
        if(num > 0){
            return CommonResult.success("新增成功");
        }else if(num == -1){
            return CommonResult.failed("同一时段内只允许存在一种考勤规则");
        }else {
            return CommonResult.failed("新增失败");
        }
    }

    @PostMapping("/update")
    public CommonResult updateRules(@RequestBody AttendanceRules rules){
        int num = attendanceRulesService.updateRules(rules);
        if(num > 0){
            return CommonResult.success("更新成功");
        }else {
            return CommonResult.failed("更新失败");
        }
    }

    @DeleteMapping("/delete/{id}")
    public CommonResult deleteRules(@PathVariable Long id){
        int num = attendanceRulesService.deleteRules(id);
        if(num > 0){
            return CommonResult.success("删除成功");
        }else {
            return CommonResult.failed("删除失败");
        }
    }

    @GetMapping("/findAttendanceRulesById/{id}")
    public CommonResult findAttendanceRulesById(@PathVariable Long id){
        AttendanceRules rules = attendanceRulesService.findAttendanceRulesById(id);
        if(rules != null){
            return CommonResult.success(rules);
        }else {
            return CommonResult.failed("未能查到数据");
        }
    }

}
